<?php
/**
 * Created by PhpStorm.
 * User: 11870
 * Date: 2017/9/13
 * Time: 18:35
 */
namespace Api\Controller;
use Api\Model\CartModel;
use Api\Model\DocumentGoods;
use Api\Model\DocumentGoodsModel;
class Cart extends \think\Controller
{
    private $_exm;
    public function _initialize(){
        parent::_initialize();
        $this->_exm = new CartModel();
    }
    //查询购物车
    public function selectCart()
    {
        $result = $this->_exm->selectCart();
        switch ($result)
        {
            case 0:
                $this->response(['status'=>0,'msg'=>'购物车为空'], 'json');
                break;
            default:
                $this->response(['status'=>1,'data'=>$result], 'json');
                break;
        }
    }
    //添加到购物车
    public function addCart()
    {
        $data = file_get_contents('php://input');
        $data = json_decode($data,true);
        if($data['num'] <= 0)
        {
            $this->response(['status'=>0,'msg'=>'库存不够,添加失败'], 'json');
        }
        $result = $this->_exm->addCart($data);

        switch ($result)
        {
            case 0:
                $this->response(['status'=>2,'data'=>'该商品已经在你的购物车中,商品数目加1'], 'json');
                break;
            case 1:
                $this->response(['status'=>1,'msg'=>'添加成功'], 'json');
                break;
            case 2:
                $this->response(['status'=>0,'msg'=>'库存不够,添加失败'], 'json');
                break;
        }
    }
    //加减购物车中的商品数量
    public function addGoodsNum()
    {
        $data = I('param.');
        $data = file_get_contents('php://input');
        $data = json_decode($data,true);

        //检查当前商品的库存是否足够
        $id = $data['gid'];
        $handle = $data['handle'];
        $count = $handle?$data['count']+1:$data['count']-1;
        $good = new DocumentGoodsModel();
        $jugderes = $good->judgeNum($count,$id);
        if(!$jugderes){
            $this->response(['status'=>0,'msg'=>'库存不够啦'], 'json');
        }
        $result = $this->_exm->addGoodsNum($id,$handle);
        switch ($result)
        {
            case 1:
                $this->response(['status'=>1,'msg'=>'数目加1'], 'json');
                break;
            default:
                $this->response(['status'=>2,'msg'=>'数目减1'], 'json');
                break;
        }
    }
    //直接修改购物车中商品中的数量
    public function chgGoodNum()
    {
        $data = I('param.');
        $data = file_get_contents('php://input');
        $data = json_decode($data,true);
        $id = $data['gid'];
        $newcount = $data['newcount'];
        $result = $this->_exm->chgGoodNum($newcount,$id);
        switch ($result)
        {
            case 1:
                $this->response(['status'=>1,'msg'=>'数量改变成功'], 'json');
                break;
            default:
                $this->response(['status'=>2,'msg'=>'数量改变失败'], 'json');
                break;
        }
    }

    //从购物车中移除
    public function removeCart()
    {
        $data = file_get_contents('php://input');
        $data = json_decode($data,true);
        $id = $data['gid'];
        $result = $this->_exm->removeCart($id);
        switch ($result)
        {
            case 1:
                $this->response(['status'=>1,'msg'=>'移除成功'], 'json');
                break;
            default:
                $this->response(['status'=>0,'msg'=>'移除失败'], 'json');
                break;
        }
    }
    //删除购物车
    public function clearCart()
    {
        $result = $this->_exm->clearCart();
        switch ($result)
        {
            case 0:
                $this->response(['status'=>0,'msg'=>'清空购物车失败'], 'json');
                break;
            case 1:
                $this->response(['status'=>1,'msg'=>'清空购物车成功'], 'json');
                break;
        }
    }
}